//
//  ReconciliationViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/4/1.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#define kLeftTableView 500
#define kRightTableView 600

#import "ReconciliationViewController.h"
#import "OrderCalendarView.h"
#import "ReconciliationRotalCell.h"
#import "ReconciliationCell.h"
#import "ReconciliationMenuView.h"
#import "WithdrawRecordViewController.h"
#import "WithdrawalExplainViewController.h"
#import "SettlementWayViewController.h"
#import "UserInfo.h"
@interface ReconciliationViewController ()<UITableViewDelegate,UITableViewDataSource,ReconciliationMenuViewDelegate>
@property (nonatomic ,strong) UserInfo *userInfo;
@property (nonatomic ,strong) UITableView *leftTableView;
@property (nonatomic ,strong) UITableView *rightTableView;
@property (nonatomic ,strong) ReconciliationMenuView *menuView;
@property (nonatomic ,strong) ReconciliationCell *reconciliationCell;
@property (nonatomic ,strong) ReconciliationRotalCell *reconciliationRotalCell;
@property (nonatomic ,strong) NSArray *titles;
@property (nonatomic ,strong) NSArray *images;
@property (nonatomic ,strong) NSString *selectTime;
@property (nonatomic ,assign) BOOL isSudit;

@end

@implementation ReconciliationViewController

- (NSArray *)titles {
    
    if (!_titles) {
        _titles = @[@"结算方式",@"提现记录"];
    }
    return _titles;
}

- (NSArray *)images {
    if (!_images) {
        _images = @[@"shoukuanfangshi",@"jilu"];
    }
    return _images;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"对账查询";
    _userInfo = [UserInfo sharedUserInfo];
    // 默认进来选中的是查账
    _isSudit = YES;
    [self setNarBarUI];
    [self createMenuViewUI];
    [self createLeftTableViewUI];
 
   
}
- (void)setNarBarUI {

    UIBarButtonItem *calendarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"rili"] style:(UIBarButtonItemStylePlain) target:self action:@selector(calendarItemClicked:)];
    self.navigationItem.rightBarButtonItem = calendarItem;

 }
#pragma mark - 日历按钮
- (void)calendarItemClicked:(UIBarButtonItem *)sender {
    
    NSLog(@"日历");
    OrderCalendarView *orderCanlendarView = [[OrderCalendarView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [orderCanlendarView showCustomCalendarView];
    orderCanlendarView.selectTime = ^(NSString *time){
        
        self.selectTime = time;
    };
}

#pragma mark - 提现说明
- (void)instructionsItemClicked:(UIBarButtonItem *)sender {
    
    WithdrawalExplainViewController *explainVC = [[WithdrawalExplainViewController alloc] init];
    [self.navigationController pushViewController:explainVC animated:YES];
}


#pragma mark - 查账和提现菜单
- (void)createMenuViewUI {

    self.menuView = [[ReconciliationMenuView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 46))];
    self.menuView.delegate =self;
    [self.view addSubview:self.menuView];
    
}

-(void)buttonClickWithTag:(NSInteger)index {
    
    switch (index) {
        case 200:
        {
            NSLog(@"查账");
            _isSudit = YES;
            [self setNarBarUI];
            [self.rightTableView removeFromSuperview];
            [self createLeftTableViewUI];
        }
            break;
        case 201:
        {
            NSLog(@"提现");
            _isSudit = NO;
            [self.leftTableView removeFromSuperview];
            [self createRightTableViewUI];
          
        }
            break;
        default:
            break;
    }
 
}


- (void)createLeftTableViewUI {
    self.leftTableView =[[UITableView alloc] initWithFrame:(CGRectMake(0, 46, size_width, size_height-110))];
    self.leftTableView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.leftTableView.tag = kLeftTableView;
    [self.view addSubview:self.leftTableView];
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, 8))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.leftTableView.tableHeaderView = headerView;
    
    
    // 注册总计收入cell
    [self.leftTableView registerNib:[UINib nibWithNibName:@"ReconciliationRotalCell" bundle:nil] forCellReuseIdentifier:@"ReconciliationRotalCell"];
    
    // 注册个人消费cell
    [self.leftTableView registerNib:[UINib nibWithNibName:@"ReconciliationCell" bundle:nil] forCellReuseIdentifier:@"ReconciliationCell"];
}

- (void)createRightTableViewUI {
    
    // 提现说明
    UIBarButtonItem *instructionsItem = [[UIBarButtonItem alloc] initWithTitle:@"提现说明" style:(UIBarButtonItemStylePlain) target:self action:@selector(instructionsItemClicked:)];
    self.navigationItem.rightBarButtonItem = instructionsItem;
    
    self.rightTableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 46, size_width, size_height))];
    self.rightTableView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.scrollEnabled = NO;
    self.rightTableView.tag = kRightTableView;
    [self.view addSubview:self.rightTableView];
    
    [self createHeaderViewUI];
}


- (void)createHeaderViewUI {
    
    UIView *headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width , 195))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.rightTableView.tableHeaderView = headerView;
    
    // 背景色
    UIView *backView =[[UIView alloc] initWithFrame:(CGRectMake(0, 8, size_width, 187))];
    backView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:backView];
    
    // 金额
    NSString *moneyStr = [NSString stringWithFormat:@"%@.00",_userInfo.Withdrawals_lower_limit];
    UILabel *moneyLabel = [UILabel setLabelWithText:[NSString stringWithFormat:@"¥ %@",moneyStr] fontSize:18 color:[UIColor colorWithHexString:@"#333333"]];
    moneyLabel.frame = CGRectMake(0, 35, size_width, 30);
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:moneyLabel.text];
    [totalStr setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#333333"]} range:[moneyLabel.text rangeOfString:[NSString stringWithFormat:@"%@",moneyStr]]];
    moneyLabel.attributedText = totalStr;
    [headerView addSubview:moneyLabel];
    
    // 当前可提现金额
    UILabel *wdlMoney = [UILabel setLabelWithText:@"当前可提现金额" fontSize:12 color:[UIColor colorWithHexString:@"#999999"]];
    wdlMoney.frame = CGRectMake(0, moneyLabel.frame.size.height + moneyLabel.frame.origin.y + 8, size_width, 12);
    wdlMoney.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:wdlMoney];
    
    // 线条
    UIView *lineView = [[UIView alloc] initWithFrame:(CGRectMake((size_width - 296)/2, 106, 296, 1))];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#dddddd"];
    [backView addSubview:lineView];
    
    // 我要提现按钮
    UIButton *withdrawalBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    withdrawalBtn.frame = CGRectMake((size_width - 296)/2, headerView.frame.size.height - 62, 296, 44);
    withdrawalBtn.backgroundColor = [UIColor colorWithHexString:@"#ff9900"];
    [withdrawalBtn setTitle:@"我要提现" forState:(UIControlStateNormal)];
    [withdrawalBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    withdrawalBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    withdrawalBtn.layer.masksToBounds = YES;
    withdrawalBtn.layer.cornerRadius = 5;
    [withdrawalBtn addTarget:self action:@selector(withdrawalBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [backView addSubview:withdrawalBtn];
    
    NSInteger currentMoney = [moneyStr integerValue];
    if (currentMoney < 100) {
        withdrawalBtn.backgroundColor = [UIColor colorWithHexString:@"#dddddd"];
        [withdrawalBtn setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:(UIControlStateNormal)];
    }
    
}

#pragma mark - 我要提现按钮
- (void)withdrawalBtnClick:(UIButton *)sender {
    
    NSLog(@"我要提现按钮");
    [self showAlertWith:@"该功能尚未开放！"];
}



#pragma mark - UITableView的代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (tableView.tag == kLeftTableView) {
        return 2;
    }else{
        return 1;
    }
   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView.tag == kLeftTableView) {
        switch (section) {
            case 0:
                return 1;
                break;
            case 1:
                return 10;
                break;
            default:
                break;
        }
    }else{
        return 2;
    }
    
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == kLeftTableView) {
        if (indexPath.section == 0) {
            ReconciliationRotalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReconciliationRotalCell" forIndexPath:indexPath];
            
//            cell.timeLabel.text = [NSString stringWithFormat:@"%@",self.selectTime];
//            [self.leftTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            return cell;
        }else{
            ReconciliationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReconciliationCell" forIndexPath:indexPath];
            return cell;
        }

    }else {
        
        static NSString *withdrawalCellID = @"withdrawalCellID";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:withdrawalCellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:withdrawalCellID];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }
        cell.textLabel.text = self.titles[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = color_textOne;
        cell.imageView.image = [UIImage imageNamed:self.images[indexPath.row]];
        return cell;
    }
    
    
    return [[UITableViewCell alloc] init];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == kLeftTableView) {
        if (indexPath.section == 1) {
            return 72;
        }
    }
    
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (tableView.tag == kLeftTableView) {
        return 0;
    }else{
        return 8;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (tableView.tag == kLeftTableView) {
        if (section == 0) {
            return 8;
        }
    }else{
        return 0;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView.tag == kLeftTableView) {
       
    }else{
        if (indexPath.row == 0) {
            SettlementWayViewController *settlementVC = [[SettlementWayViewController alloc] init];
            [self.navigationController pushViewController:settlementVC animated:YES];
        }else{
            WithdrawRecordViewController *withrawVC = [[WithdrawRecordViewController alloc] init];
            [self.navigationController pushViewController:withrawVC animated:YES];
        }

    }
}

@end
